#include "linkslist.h"



LinksList::LinksList(void)
{
  StartingLink = NULL;
  TailLink = NULL;
  std::cout << "Creating a LinksList" << std::endl;
}

LinksList::~LinksList(void)
{
  std::cout << "Closing a LinksList" << std::endl;
}

bool * LinksList::CreateNewLink(void)
{
  Link * LinkBuff;
  LinkBuff = new Link;

  LinkBuff->DataLine = 0;
  LinkBuff->NextLink = NULL;

  /* check if this is first node in the list */
  if(NULL == StartingLink)
  {
    /* set addressing to start list */
    StartingLink = LinkBuff;
    TailLink = LinkBuff;
  }
  else
  {
    /* correctly update linked list with new items */
    TailLink->NextLink = LinkBuff;
    TailLink = LinkBuff;
  }

  /* retun pointer to data line bus */
  return &LinkBuff->DataLine;
}

void LinksList::DestroyAllLinks(void)
{
  Link * LinkBuff;

  while(StartingLink != NULL)
  {
    /* create clone of node for deletion */
    LinkBuff = StartingLink;

    /* check to see if we are at the end of the list */
    if(StartingLink->NextLink != NULL)
    {
      /* set pointer to next node in the list */
      StartingLink = StartingLink->NextLink;
    }
    else
    {
      /* clear node for exit of the while loop */
      StartingLink = NULL;
    }


    /* remove linked list node */
    delete LinkBuff;
  }
      
}
